package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;

import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.PermissionService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;

import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    //容器对象
    @Autowired
    private ApplicationContext ctx;

    @Override
    public void insert(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {

        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> selectAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPagSize());

        return new PageInfo<>(permissionMapper.queryForList(qo));


    }

    @Override
    public void reload() {
            //查询数据库中所有的权限表达式
        List<String> expressions = permissionMapper.selectAllExpression();
        //获取容器对象中贴有@Controller注解的 bean
        //key是bean id  value是控制对象
        Map<String, Object> beanMap = ctx.getBeansWithAnnotation(Controller.class);
        //获取控制器的处理方法
        Collection<Object> ControllerBeans = beanMap.values();
        for (Object controllerBean : ControllerBeans) {
            //获取字节码对象
            Class<?> controllerBeanClass = controllerBean.getClass().getSuperclass();
            //获取自己包括他爹的方法
            //  Method[] methods = controllerBeanClass.getMethods();


            //自己的方法
            Method[] declaredMethods = controllerBeanClass.getDeclaredMethods();
            for (Method method : declaredMethods) {
                //若有方法贴了我们自定义注解就放回true没有就是false
                if (method.isAnnotationPresent(RequiresPermissions.class)) {
                    //System.out.println(method);
                    RequiresPermissions requiredPermissionAnno = method.getAnnotation(RequiresPermissions.class);
                    String [] nameAnExpress = requiredPermissionAnno.value();
                    String expression =nameAnExpress[0];
                    String name =nameAnExpress[1];
                    //插入前判断,是否在数据库中已经纯在该权限
                    if (!expressions.contains(expression)){//取反如果false就添加
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }

                }
            }
        }


    }

    @Override
    public List<String> querByEmployeeId(Long employeeId) {
        return permissionMapper.querByEmployeeId(employeeId);
    }




}
